package cl.mt.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.StringTokenizer;

public class Utiles {

	private static SimpleDateFormat sdf =
		new SimpleDateFormat("dd/MM/yyyy HH:mm");

	public static String[] split(String string, char sep) {
		String separador = String.valueOf(sep);
		StringTokenizer st = new StringTokenizer(string, separador);

		int nTokens = st.countTokens();
		if (nTokens == 0)
			return null;

		String[] out = new String[nTokens];
		for (int i = 0; i < out.length; i++) {
			out[i] = st.nextToken();
		}
		return out;
	}

	public static String[] split(String str, String delim) {
		if (str == null)
			return null;
		StringTokenizer st = new StringTokenizer(str, delim);
		if (st == null)
			return null;

		int n = st.countTokens();
		if (n == 0)
			return null;

		String[] arr = new String[n];
		for (int i = 0; st.hasMoreTokens();) {
			arr[i++] = st.nextToken();
		}
		return arr;
	}

	public static String sinNull(Object o, String def) {
		if (o == null)
			return def;
		return o.toString();
	}

	public static String formatFecha(Date fecha) {
		if (sdf == null)
			sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		try {
			return sdf.format(fecha);
		} catch (Exception e) {
		}
		return null;
	}

	public static String formatFecha(Date fecha, String formato) {
		SimpleDateFormat sdf1 = new SimpleDateFormat(formato);
		try {
			return sdf1.format(fecha);
		} catch (Exception e) {
		}
		return "";
	}

	public static Integer parseInteger(String str) {
		if (str == null || str.length() == 0)
			return null;

		try {
			return new Integer(str);
		} catch (NumberFormatException e) {
		}
		return null;
	}

	public static Long parseLong(String str) {
		if (str == null || str.length() == 0)
			return null;

		try {
			return new Long(str);
		} catch (NumberFormatException e) {
		}
		return null;
	}

	public static Date getFechaHoy() {
		Calendar c = Calendar.getInstance();
		return c.getTime();
	}

	public static Date addDiasFecha(Date fechaIni, Integer cantidadDias) {
		System.out.println(
			"Tiempo ["
				+ fechaIni.getTime()
				+ ","
				+ (cantidadDias.intValue())
				+ "]");
		long sumar = (cantidadDias.longValue() * (long) 24 * (long) 3600000);
		System.out.println("Tiempo [" + fechaIni.getTime() + "," + sumar + "]");
		return new Date(fechaIni.getTime() + sumar);
	}

	private static SimpleDateFormat sdfFecha =
		new SimpleDateFormat("yyyyMMddHHmmss");
	private static SimpleDateFormat sdfDia = new SimpleDateFormat("yyyyMMdd");
	private static SimpleDateFormat sdfHora = new SimpleDateFormat("HHmmss");

	public static String formatDia(Date fecha) {
		if (fecha == null)
			return "";
		return sdfDia.format(fecha);
	}

	/**
	 * @param string
	 * @return
	 */
	public static Date stringToDate(String string) {
		// TODO Ap�ndice de m�todo generado autom�ticamente
		return null;
	}

	/**
	 * @param string
	 * @return
	 */
	public static String formatString(String str, int largo) {
		if (str == null)
			return "";

		return (str.length() > largo ? str.substring(0, largo) : str);
	}

	public static String formatPrecio(Integer obj) {
		if (obj == null)
			return "";
		int cantidad = obj.intValue();
		String ret = "";
		while (true) {
			int res = cantidad % 1000;
			cantidad = cantidad / 1000;

			if (cantidad == 0) {
				ret = "" + res + ret;
				break;
			}
			ret = "." + filledLeft(res, "0", 3) + ret;
		}

		return ret;
	}

	private static String filledLeft(int i, String c, int largo) {
		String ret = "" + i;

		while (ret.length() < largo) {
			ret = c + ret;
		}
		return ret;
	}

	public static long comparaFecha(Date fechaIni, Date fechaFin) {
		System.out.println(
			"[" + (fechaIni.getTime() - fechaFin.getTime()) + "]");
		if (fechaIni == null)
			return new Long(1).longValue();
		if (fechaFin == null)
			return new Long(-1).longValue();

		return ((fechaIni.getTime() - fechaFin.getTime()));
	}
}
